#coding=utf-8

# Create your views here.
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse,HttpResponseRedirect
from django.http import Http404
from django.conf import settings
from mediacom.bgcms.models import News,NewsType,PhotoCategory,PhotoGallery,PhotoMetaInfo
from mediacom.bgcms.forms import GuestBookForm,SignUpForm
from mediacom.bgcms.img import getThumbDimensions,getImageInfo
   
def index(request):    
    if request.method == 'GET':    
        try:
            notify_list = {}
            notify_list = News.objects.filter(is_notify__exact=True)[:5]
            
            industry_news = {}
            industry_news = News.objects.filter(news_type__type__exact='行业动态')[:5]
            
            course_list = {}
            course_list = News.objects.filter(news_type__type__exact='热点课程')[:5]
            
            test_list = {}
            test_list = News.objects.filter(news_type__type__exact='模拟试题')[:5]
            
            health_spread_list = {}
            health_spread_list = News.objects.filter(news_type__type__exact='健康文化传播与推广')[:5]
            
            health_product_list = {}
            health_product_list = News.objects.filter(news_type__type__exact='健康文化产品推广')[:5]
            
            sport_spread_list = {}
            sport_spread_list = News.objects.filter(news_type__type__exact='体育文化传播与推广')[:5]
            
            sport_product_list = {}
            sport_product_list = News.objects.filter(news_type__type__exact='体育文化产品推广')[:5]
            
        except News.DoesNotExist:
            pass;
            #raise Http404
        return render_to_response('index.html', \
                                  {'notify_list': notify_list, \
                                   'industry_news':industry_news,\
                                   'course_list':course_list,\
                                   'test_list':test_list,\
                                   'health_spread_list':health_spread_list,\
                                   'health_product_list':health_product_list,\
                                   'sport_spread_list':sport_spread_list,\
                                   'sport_product_list':sport_product_list,\
                                   'album_list':get_album_list(request)[0:4]})
        
def guestbook_add(request):
    if request.method == 'POST':
        form = GuestBookForm(request.POST)
        if form.is_valid():
            record = form.save()
            return HttpResponse('谢谢!')
    else:
        form = GuestBookForm()
    
    return render_to_response('guestbook_add.html', {'form':form})
    
def signup_add(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            record = form.save()
            return HttpResponse('报名成功!')
            #return HttpResponseRedirect('/signup/%d'%record.id)
    else:
        form = SignUpForm()
    
    return render_to_response('signup.html', {'form':form})
    
def get_album_list(request):
    if request.method == 'GET':
        category_list = PhotoCategory.objects.all()
        photo_list = PhotoGallery.objects.all()
        for p in photo_list:
            try:
                PhotoMetaInfo.objects.get(id__exact=p.id)                
            except PhotoMetaInfo.DoesNotExist:
                try:
                    photo_info = getImageInfo(p.img.read())
                    dimension = getThumbDimensions(photo_info[1],photo_info[2],250)
                    print '-----------------',dimension
                    info = PhotoMetaInfo(photo_id=p,width=dimension[0],height=dimension[1])
                    print info
                    info.save()
                    p.img.close()
                except IOError:
                    pass
                
    album_list = []
    for category in category_list:
        try:
            photo_list = PhotoGallery.objects.filter(photo_type__exact=category)
            if photo_list.count() != 0:
                photo = photo_list[0]
                if PhotoMetaInfo.objects.filter(photo_id__exact=photo).count() != 0:
                    info = PhotoMetaInfo.objects.filter(photo_id__exact=photo)[0]
                    album = [category, photo, info, photo_list.count()]
                    album_list.append(album)
        except PhotoGallery.DoesNotExist,PhotoMetaInfo.DoesNotExist:
            pass
        
    return album_list
    
def show_album(request):
    
    return render_to_response('photoalbum.html',{'album_list':get_album_list(request)})
    
def show_photos(request, id):
    if request.method == 'GET':
        #content = ''
        #for t in PhotoCategory.objects.all():
        #    photo_list = PhotoGallery.objects.filter(photo_type=t)
        #   content +='<h1>%s</h1>'%t.type
        #   for photo in photo_list:
        #        content+= '<li><a href="%s">%s</a></li>'%(photo.img.url,photo.desc)
        #ontent = '<fuck all>'
        #return render_to_response('photogallery.html',{'content':content})
        photo_list = PhotoGallery.objects.all().filter(photo_type__id__exact=id)
        for p in photo_list:
            try:
                PhotoMetaInfo.objects.get(id__exact=p.id)                
            except PhotoMetaInfo.DoesNotExist:
                try:
                    photo_info = getImageInfo(p.img.read())
                    dimension = getThumbDimensions(photo_info[1],photo_info[2],250)
                    print '-----------------',dimension
                    info = PhotoMetaInfo(photo_id=p,width=dimension[0],height=dimension[1])
                    print info
                    info.save()
                    p.img.close()
                except IOError:
                    pass
        
        metainfo_list = PhotoMetaInfo.objects.values('photo_id','height','width').distinct()   
            
        return render_to_response('photogallery.html',{'photo_list':photo_list,'metainfo_list':metainfo_list})